package cc.wsyw126.java.collection.map;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by junpeng.yang on 16/8/31.
 */
public class HashtableConcurrentHashMap {
    public static Map<Integer,String> addConcurrentHashMap(){
        Long start = System.currentTimeMillis();
        ConcurrentHashMap<Integer,String> concurrentHashMap = new ConcurrentHashMap<>();
        for (int i = 0; i < 1000000; i++) {
            concurrentHashMap.put(i,i+"");
        }
        System.out.println("HashtableConcurrentHashMap.addConcurrentHashMap :" + (System.currentTimeMillis() - start));
        return concurrentHashMap;
    }

    public static Map<Integer,String> addHashtable(){
        Long start = System.currentTimeMillis();
        Hashtable<Integer,String> hashtable = new Hashtable<>();
        for (int i = 0; i < 1000000; i++) {
            hashtable.put(i,i+"");
        }
        System.out.println("HashtableConcurrentHashMap.addHashtable :" + (System.currentTimeMillis() - start));
        return hashtable;
    }

    public static Map<Integer,String> addSyn(){
        Long start = System.currentTimeMillis();
        HashMap<Integer,String> hashMap = new HashMap<>();
        for (int i = 0; i < 1000000; i++) {
            hashMap.put(i,i+"");
        }
        Map map = Collections.synchronizedMap(hashMap);
        System.out.println("HashtableConcurrentHashMap.addSyn :" + (System.currentTimeMillis() - start));
        return map;
    }

    public static void readMap(Map map){
        Long start = System.currentTimeMillis();
        Iterator iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry entry = (Map.Entry) iterator.next();
        }
        System.out.println("HashtableConcurrentHashMap.readMap :" + (System.currentTimeMillis() - start));
    }


    public static void main(String[] args) {
        Map map = addConcurrentHashMap();
        readMap(map);
        map = addHashtable();
        readMap(map);

        map = addSyn();
        readMap(map);

    }

}
